package org.jeecg.modules.pay.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.boot.starter.lock.annotation.JLock;
import org.jeecg.boot.starter.lock.annotation.JRepeat;
import org.jeecg.boot.starter.rabbitmq.client.RabbitMqClient;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.base.BaseMap;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.pay.entity.UserAccount;
import org.jeecg.modules.pay.service.IUserAccountService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 用户余额表
 * @Author: jeecg-boot
 * @Date:   2022-06-14
 * @Version: V1.0
 */
@Api(tags="用户余额表")
@RestController
@RequestMapping("/com.pay/userAccount")
@Slf4j
public class UserAccountController extends JeecgController<UserAccount, IUserAccountService> {
	@Autowired
	private IUserAccountService userAccountService;

	 @Autowired
	 private RabbitMqClient rabbitMqClient;
	
	/**
	 * 管理员查看所有用户账户信息
	 *
	 * @param userAccount
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@JRepeat(lockKey = "#name", lockTime = 2)
	//@AutoLog(value = "用户余额表-分页列表查询")
	@ApiOperation(value="用户余额表-分页列表查询", notes="用户余额表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<UserAccount>> queryPageList(UserAccount userAccount,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<UserAccount> queryWrapper = QueryGenerator.initQueryWrapper(userAccount, req.getParameterMap());
		IPage<UserAccount> pageList = userAccountService.getPage(pageNo, pageSize, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 查看所有自己的账户信息
	  *
	  * @param userAccount
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "用户余额表-分页列表查询")
	 @ApiOperation(value="查询自己的余额", notes="用户余额自己的余额")
	 @GetMapping(value = "/myselt")
	 @JRepeat(lockKey = "#name", lockTime = 2)
	 public Result<IPage<UserAccount>> myself(UserAccount userAccount,
													 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													 HttpServletRequest req) {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 userAccount.setId(sysUser.getId());
		 QueryWrapper<UserAccount> queryWrapper = QueryGenerator.initQueryWrapper(userAccount, req.getParameterMap());
		 IPage<UserAccount> pageList = userAccountService.getPage(pageNo, pageSize, queryWrapper);
		 return Result.OK(pageList);
	 }

	/**
	 *   添加用户同时添加账户
	 *
	 * @param userAccount
	 * @return
	 */
	@AutoLog(value = "添加用户同时添加账户")
	@ApiOperation(value="添加用户同时添加账户", notes="添加用户同时添加账户")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody UserAccount userAccount) {
		userAccountService.add(userAccount);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  充值
	 *
	 * @param userAccount
	 * @return
	 */
	@JLock(lockKey= "redis_lock_recharge")
	@AutoLog(value = "充值")
	@ApiOperation(value="充值", notes="充值")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody UserAccount userAccount) {
		userAccountService.addMoney(userAccount.getId(), userAccount.getBalance());
		return Result.OK("充值成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "用户余额表-通过id删除")
	@ApiOperation(value="用户余额表-通过id删除", notes="用户余额表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		userAccountService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "用户余额表-批量删除")
	@ApiOperation(value="用户余额表-批量删除", notes="用户余额表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.userAccountService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "用户余额表-通过id查询")
	@JRepeat(lockKey = "#name", lockTime = 2)
	@ApiOperation(value="用户余额表-通过id查询", notes="用户余额表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<UserAccount> queryById(@RequestParam(name="id",required=true) String id) {
		UserAccount userAccount = userAccountService.getById(id);
		if(userAccount==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(userAccount);
	}

	 /**
	  * 通过名字查询
	  *
	  * @param name
	  * @return
	  */
	 //@AutoLog(value = "用户余额表-通过id查询")
	 @JRepeat(lockKey = "#name", lockTime = 2)
	 @ApiOperation(value="用户余额表-通过名字查询", notes="用户余额表-通过名字查询")
	 @GetMapping(value = "/queryByName")
	 public Result<List<UserAccount>> queryByName(@RequestParam(name="name",required=true) String name) {
		 List<UserAccount> userAccount = userAccountService.getByName(name);
		 if(userAccount==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(userAccount);
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param userAccount
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, UserAccount userAccount) {
        return super.exportXls(request, userAccount, UserAccount.class, "用户余额表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, UserAccount.class);
    }

}
